/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.pokemon.dao;

import com.pokemon.entities.Stadium;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Repository;

/**
 *
 * @author xolsak1
 */
@Component
@Repository("StadiumDAOJpa")
public class StadiumDAOJpa implements StadiumDAO{

    @PersistenceContext
    private EntityManager em;

    public void setEm(EntityManager em) {
        this.em = em;
    }
    
    
    public void addStadium(Stadium stadium) {
        
            if ((stadium == null)|| (stadium.getId() != null)) {
                throw new IllegalArgumentException("Stadium must not be null and id must be null");
            }
            em.persist(stadium);
       
    }

    
    public Stadium getStadium(Long id) {
        
            if (id == null) {
                throw new IllegalArgumentException("Stadium must not be null");
            }
            Stadium stadium = em.find(Stadium.class, id);
            return stadium;
        
    }

    
    public void updateStadium(Stadium stadium) {
        
            if ((stadium == null)|| (stadium.getId() == null)) {
                throw new IllegalArgumentException("Stadium must not be null and id must not be null");
            }
            
            em.merge(stadium);
        
    }

    
    public void removeStadium(Stadium stadium) {
        
            if ((stadium == null)|| (stadium.getId() == null)) {
                throw new IllegalArgumentException("Stadium must not be null and id must not be null");
            }
            Stadium stadiumToRemove = em.find(Stadium.class, stadium.getId());
            em.remove(stadiumToRemove);
        
    }

    
    public List<Stadium> getAllStadiums() {
        
            Query query = em.createQuery("SELECT e FROM Stadium e");
            return (List<Stadium>) query.getResultList();
        
    } 

    /* 
     * Without implementation. Just for backward compatibility.
     */
    public void setEntityManagerFactory(EntityManagerFactory entityManagerFactory) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
